package com.ziq.zcamp.bean.life

import android.view.View
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.content.res.ResourcesCompat
import com.donkingliang.groupedadapter.adapter.GroupedRecyclerViewAdapter
import com.donkingliang.groupedadapter.holder.BaseViewHolder
import com.facebook.drawee.generic.RoundingParams
import com.ziq.base.extension.click
import com.ziq.base.extension.dp
import com.ziq.zcamp.R
import com.ziq.zcamp.bean.place.DetailGroup
import com.ziq.zcamp.community.CommunityPostingAdapter
import com.ziq.zcamp.community.SubjectAdapter
import com.ziq.zcamp.databinding.ItemCampLifeEntranceBinding
import com.ziq.zcamp.life.CampLifeActivity
import com.ziq.zcamp.manager.GlobalContextManager
import com.ziq.zcamp.manager.PrivacyManager

class CampLifeEntranceGroup(val adapter: GroupedRecyclerViewAdapter) : DetailGroup() {

    companion object{
        const val TYPE_CAMP_LIFE = 3
    }

    var list: MutableList<Int>? = null

    override fun getChildrenCount(): Int {
        return if (list.isNullOrEmpty()) 0 else 1
    }

    override fun getHeaderViewType(groupPosition: Int): Int {
        return 0
    }

    override fun getChildViewType(groupPosition: Int, childPosition: Int): Int {
        return TYPE_CAMP_LIFE
    }

    override fun hasHeader(groupPosition: Int): Boolean {
        return false
    }

    override fun hasFooter(groupPosition: Int): Boolean {
        return false
    }

    override fun onBindHeaderViewHolder(
        holder: BaseViewHolder?,
        groupPosition: Int
    ) {
    }

    override fun onBindChildViewHolder(
        holder: BaseViewHolder?,
        groupPosition: Int,
        childPosition: Int
    ) {
        if(holder != null){
            val binding = ItemCampLifeEntranceBinding.bind(holder.itemView)
            if (adapter is CommunityPostingAdapter){
                val params = binding.root.layoutParams as ConstraintLayout.LayoutParams
                params.width = adapter.itemWidth
                params.setMargins(4.dp,0,4.dp,8.dp)
                binding.root.layoutParams = params
            }

            if(adapter is SubjectAdapter) {
                val params = binding.root.layoutParams as ConstraintLayout.LayoutParams
                params.width = adapter.itemWidth
                params.setMargins(0,0,8.dp,0)
                binding.root.layoutParams = params
                binding.root.hierarchy.roundingParams = RoundingParams.fromCornersRadius(7.dp.toFloat())
            }

            val value = list?.get(groupPosition)?:0

            if (value == 0){
                binding.groupOfNum.visibility = View.GONE
                binding.root.setActualImageResource(R.drawable.bg_camp_life_entrance)
            }else{
                binding.tvCampLifeEntranceNum.animationDuration = 1000L
                val last = binding.tvCampLifeEntranceNum.getText().toString().toInt()
                binding.tvCampLifeEntranceNum.setText(value.toString(),value>last)
                binding.groupOfNum.visibility = View.VISIBLE
                binding.root.setActualImageResource(R.drawable.bg_camp_life_entrance_has_data)
            }
            binding.root.click {
                GlobalContextManager.getCurrentActivity()?.let {
                    PrivacyManager.checkPrivacy(it, action = {
                        CampLifeActivity.open(GlobalContextManager.appContext!!)
                    })
                }
            }
        }
    }

    override fun onBindFooterViewHolder(
        holder: BaseViewHolder?,
        groupPosition: Int
    ) {}

}